package com.lab.es.demo.aspect;


import com.lab.es.demo.annotation.IndexHandle;
import com.lab.es.demo.dto.UpdateItemDTO;
import com.lab.es.demo.message.SendMessage;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
@Order(99)
public class IndexAspect {
    @Autowired
    private SendMessage sendMessage;

    @Pointcut("@annotation(com.lab.es.demo.annotation.IndexHandle)")
    public void annotationPoinCut() {
    }

    @AfterReturning(value = "annotationPoinCut()", returning = "returnValue")
    public void afterReturning(JoinPoint joinPoint, Object returnValue) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        IndexHandle indexHandle = method.getAnnotation(IndexHandle.class);

        Object[] args = joinPoint.getArgs();
        sendMessage.sendItem((UpdateItemDTO) args[0]);
        log.info(indexHandle.desc() + " afterReturning: 返回值: " + returnValue);
    }
}
